package com.example.demo.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("api/ass")
public class helloController {

    @GetMapping("hello")
    public String Hello()
    {
        return "Hello world 111";
    }
    @PostMapping("getName")
    public String getName()
    {
        return "邓公";
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件",notes = "上传文件")
    public String upload(String name, MultipartFile file, HttpServletRequest request) throws IOException
    {
        System.out.println(name);
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getContentType());

        String path =request.getServletContext().getRealPath("/upload/");
        System.out.println(path);
        saveFile(file,path);

        return "上传成功";
    }

    public void saveFile(MultipartFile file,String path) throws IOException
    {
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }

        File newFile =new File(path+file.getOriginalFilename());
        file.transferTo(newFile);
    }
}
